<?php
/**
 * Created by PhpStorm.
 * User: qixiuxiu
 * Date: 2017/5/6
 * Time: 11:41
 */
namespace AppBundle\Controller;

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use AppBundle\Entity\Person;
use AppBundle\Entity\Note;
use Symfony\Component\HttpFoundation\Response;

class NoteController extends Controller
{
    /**
     * @Route("/clue", name="note_clue")
     */
    public function clueAction(Request $request)
    {
        if ($request->getMethod() == 'POST') {
            $data = $request->request->all();
            //获得上传文件名称
            $file2 = $_FILES['photo_url']['name'];
            //加存储路径
            $path = "D:\phpStudy\WWW\Symfony-demo\web\img\pic";
            //字符串链接
            $file2name =$path."\\".$file2;

        if ( $data['full_name'] != '' || $data['checkbox'] != 'checked'){
        //实例化对象
        $note = new Note();
        //为person表中加记录
            $note->setPhotoUrl($file2name);
            $note->setFullName($data['full_name']);
            $note->setStatus($data['status']);
            $note->setLastKnownLocation($data['last_known_location']);
            $note->setText($data['text']);
            $note->setAuthorName($data['author_name']);
            $note->setEmailOfFoundPerson($data['email_of_found_person']);
            $note->setPhoneOfFoundPerson($data['phone_of_found_person']);
            $time = time();
            $note->setEntryDate($time);
     
        //取出了Doctrine的 entity manager 对象，它负责处理数据库的持久化（译注：写入）和取出对象的过程
        $em = $this->getDoctrine()->getManager();
        //告诉doctrine你希望（最终）存储product对象（没有语句执行,persist($product) 调用，告诉Doctrine去 "管理" $product 对象。没有引发对数据库的请求。
        $em->persist($note);

        //真正执行语句(如，INSERT 查询）)
        $em->flush();
        }else{
                try{
                    //抛出异常
                    throw $this->createNotFoundException('请填写失踪人员姓名并承诺以上发布信息真实可靠！');
                }catch (Exception $e){

                }
            }
        }
        return $this->render('AppBundle:Default:clue.html.twig');
    }
    /**
     * 资讯
     * @Route("/info", name="info")
     */
    public function infoAction(Request $request)
    {
        $em = $this->getDoctrine()->getManager();

        $notes =$em->getRepository('AppBundle:Note')->findAllOrderByName();
        $notes2 =$em->getRepository('AppBundle:Note')->findOrderByName();
        $count = count($notes);
        // replace this example code with whatever you need
        return $this->render('AppBundle:Default:Information.html.twig',array('notes' => $notes,'notes2'=>$notes2,'count'=>$count));
    }
    /**
     * 线索详情页
     * @Route("/clueinfo/{id}", name="clueinfo")
     */
    public function clueinfoAction($id)
    {
        $em = $this->getDoctrine()->getManager();

        $notes =$em->getRepository('AppBundle:Note')->find($id);
        // replace this example code with whatever you need
        return $this->render('AppBundle:Default:clueinfo.html.twig',array('note'=>$notes));
    }
   
}
